<?php
/**
 * Created by swagger_zxd.
 * User: zxd
 * Date: 2018/2/23
 * Time: 11:18
 */

namespace app\admin\model;


use think\Model;

class roomSet extends Model{

    //载入游戏
    public function loadGames(){
        $records='';
        try{
            $pdo=odbcLink();
            $sqlstr="exec JJ_Get_GameKindName";
            $sth=$pdo->query($sqlstr);
            $i=1;
            do{
                $rowset=$sth->fetchAll(\PDO::FETCH_ASSOC);
                switch ($i) {
                    case 1:
                        if(!empty($rowset)){
                            foreach ($rowset as $k=>$value) {
                                $records[$k]['kindId']=$rowset[$k]['KindID'];
                                $records[$k]['kindName']=iconv("gbk","utf-8",$rowset[$k]['KindName']);
                            }
                        }
                }
                $i++;
            }while($sth->nextRowset());
        }catch(\Exception $e){
            echo $e->getMessage();
        }
        $out=['records'=>$records];
        return json($out);

    }

    //载入房间
    public function loadRooms($kindName){
        $records='';
        try{
            $pdo=odbcLink();
            $sqlstr="exec JJ_Get_GameRoomName '".$kindName."'";
            $sql = iconv("utf-8", "gbk", $sqlstr);//sql转码
            $sth=$pdo->query($sql);
            $i=1;
            do{
                $rowset=$sth->fetchAll(\PDO::FETCH_ASSOC);
                switch ($i) {
                    case 1:
                        if(!empty($rowset)){
                            foreach ($rowset as $k=>$value) {
                                $records[$k]['serverId']=$rowset[$k]['ServerID'];
                                $records[$k]['roomName']=iconv("gbk","utf-8",$rowset[$k]['RoomName']);
                            }
                        }
                }
                $i++;
            }while($sth->nextRowset());
        }catch(\Exception $e){
            echo $e->getMessage();
        }
        $out=['records'=>$records];
        return json($out);

    }

    /**
     * 载入血池控制数据
     * @param $kindId
     * @param $serverId
     * @param $state
     * @param $page
     * @return \think\response\Json
     */
    public function loadXueChiKongZhi($kindId,$serverId,$state,$page){
        $recordes='';
        try{
            $pdo=odbcLink();
            $sqlstr="exec Bg_SelRoomPutScoreTable ".$kindId.", ".$serverId.", ".$state.", ".$page;

            $sql = iconv("utf-8", "gbk", $sqlstr);//sql转码
//            echo $sql;
//            exit();
            $sth=$pdo->query($sql);
            $i=1;
            do{
                $rowset=$sth->fetchAll(\PDO::FETCH_ASSOC);
                switch($i){
                    case 2:
                        $count=$rowset[0][""];
                        $pageInfo=['count'=>$count,'page'=>$page];
                        break;
                    case 3;
                        if(!empty($rowset)){
                            foreach ($rowset as $k=>$value) {
                                $recordes[$k]['runTime']=$rowset[$k]['RunTime'];
                                $recordes[$k]['serverid']=$rowset[$k]['serverid'];
                                $recordes[$k]['roomname']=iconv("gbk","utf-8",$rowset[$k]['roomname']);
                                $recordes[$k]['maxEatScore']=$rowset[$k]['MaxEatScore'];
                                $recordes[$k]['bloodChange']=$rowset[$k]['BloodChange'];
                                $recordes[$k]['state']=iconv("gbk","utf-8",$rowset[$k]['IsRun']);
                            }
                        }
                        break;
                }
                $i++;
            }while($sth->nextRowset());
        }catch(\Exception $e){
            echo $e->getMessage();
        }
        $out=['records'=>$recordes,'pageInfo'=>$pageInfo];
        return json($out);
    }

    /**
     * 历史吞吐率
     * @param $kindId
     * @param $serverId
     * @param $page
     * @return \think\response\Json
     */
    public function loadHistoricalThroughPut($kindId,$serverId,$page){
        $recordes='';
        try{
            $pdo=odbcLink();
            $sqlstr="exec Bg_SelRoomTTLList1 ".$kindId.", ".$serverId.",".$page;

            $sql = iconv("utf-8", "gbk", $sqlstr);//sql转码
//            echo $sql;
//            exit();
            $sth=$pdo->query($sql);
            $i=1;
            do{
                $rowset=$sth->fetchAll(\PDO::FETCH_ASSOC);
                switch($i){
                    case 2:
                        $count=$rowset[0]["c"];
                        $pageInfo=['count'=>$count,'page'=>$page];
                        break;
                    case 3;
                        $zsy=$rowset[0]['zsy'];
                        $countInfo=['zsy'=>$zsy];
                        break;
                    case 4;
                        if(!empty($rowset)){
                            foreach ($rowset as $k=>$value) {
                                $recordes[$k]['id']=$rowset[$k]['ID'];
                                $recordes[$k]['date']=$rowset[$k]['RecordData'];
                                $recordes[$k]['kindId']=$rowset[$k]['kindID'];
                                $recordes[$k]['roomname']=iconv("gbk","utf-8",$rowset[$k]['RoomName']);
                                $recordes[$k]['serverId']=$rowset[$k]['ServerID'];
                                $recordes[$k]['todayScore']=$rowset[$k]['TodayScore'];
                                $recordes[$k]['eatScore']=$rowset[$k]['EatScore'];
                                $recordes[$k]['outScore']=$rowset[$k]['OutScore'];
                                $recordes[$k]['ttl']=$rowset[$k]['ttl'];
                            }
                        }
                        break;
                }

                $i++;
            }while($sth->nextRowset());
        }catch(\Exception $e){
            echo $e->getMessage();
        }
        $out=['records'=>$recordes,'pageInfo'=>$pageInfo,'countInfo'=>$countInfo];
        return json($out);
    }

    /**
     * 血池变化
     * @param $serverId
     * @param $begin
     * @param $end
     * @return \think\response\Json
     */
    public function loadRoomBloodChangeChartDatas($serverId,$begin,$end){
        $records='';
        try{
            $pdo=odbcLink();
            $sqlstr="exec Bg_GameRoomBloodRecord ".$serverId.", ".$begin.", ".$end;
            $sth=$pdo->query($sqlstr);
            $i=1;
            do{
                $rowset=$sth->fetchAll(\PDO::FETCH_ASSOC);
                switch($i){
                    case 1;
                        if(!empty($rowset)){
                            foreach ($rowset as $k=>$value) {
                                $recordes[$k]['x']=$rowset[$k]['dt'];
                                $recordes[$k]['y']=$rowset[$k]['Blood_Pool'];
                                $recordes[$k]['z']=$rowset[$k]['lMaxEatScore'];
                            }
                        }
                        break;
                }
                $i++;
            }while($sth->nextRowset());
        }catch(\Exception $e){
            echo $e->getMessage();
        }
        $out=['chartDatas'=>$records];
        return json($out);
    }

    /**
     * 添加血池变化控制
     * @param $serverid 服务id
     * @param $setTime 执行时间
     * @param $score 分数
     * @param $ip ip
     * @return string
     */
    public function addRoomSet($serverid,$setTime,$score,$ip){

        try{
            $pdo=odbcLink();
            $sqlstr="declare @count int exec @count = Bg_InsertRoomPz '55', '".$serverid."', '".$setTime."', '".$score."', '".$ip."', '' select @count as c";
            $res=$pdo->exec($sqlstr);
            if($res==1){
                return "1";
            }else{
                return "error";
            }
        }catch(\Exception $e){
            echo $e->getMessage();
        }
    }

    public function changeBloodRate($serverId,$xc_rate){
        try{
            $pdo=odbcLink2();
            $sqlstr="update GameRoomItem set Revenue =".$xc_rate."where ServerID =".$serverId;
            $res=$pdo->exec($sqlstr);
            if($res==1){
                return "1";
            }else{
                return "error";
            }
        }catch(\Exception $e){
            echo $e->getMessage();
        }
    }


}